package com.bwie.doctor.service.impl;

import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.doctor.domain.dto.AddGroupDTO;
import com.bwie.doctor.domain.dto.MessageRequest;
import com.bwie.doctor.domain.po.DoctorGroupPo;
import com.bwie.doctor.domain.vo.DoctorGroupVo;
import com.bwie.doctor.domain.vo.GroupPatientVo;
import com.bwie.doctor.domain.vo.MessageResponse;
import com.bwie.doctor.mapper.DoctorPatientMapper;
import com.bwie.doctor.service.DoctorPatientService;
import com.bwie.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DoctorPatientServiceImpl implements DoctorPatientService {
    /**
     * 注入
     */
    @Autowired
    private DoctorPatientMapper mapper;

    /**
     * 医生获取患者分组列表
     * @return
     */
    @Override
    public List<DoctorGroupVo> getDoctorGroup() {
        //获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //医生获取患者分组表
        List<DoctorGroupVo> list = mapper.getDoctorGroup(loginUser.getUserid());
        //判断是否为空
        if (list == null) {
            //抛异常
            throw new RuntimeException("分组信息错误");
        }
        //返回
        return list;
    }

    /**
     * 医生获得分组下患者列表
     * @return
     */
    @Override
    public List<GroupPatientVo> getGroupPatient(Integer id) {
        //获取分组下患者列表
        List<GroupPatientVo> list = mapper.getGroupPatient(id);
        //非空判断
        if (list == null) {
            //抛异常
            throw new RuntimeException("该分组下没有患者");
        }
        //返回
        return list;
    }

    /**
     * 医生删除分组
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delGroup(Integer id) {
        //从分组列表中查找该分组
        DoctorGroupPo doctorGroupPo = mapper.getDoctorGroupById(id);
        //判断该分组是否存在
        if (doctorGroupPo == null) {
            //抛异常
            throw new RuntimeException("该分组不存在");
        }
        //医生删除分组
        int i = mapper.delGroup(id);
        //判断是否删除成功
        if (i<=0){
            //抛异常
            throw new RuntimeException("删除失败");
        }
        //删除该分组下患者
        mapper.delGroupPatient(id);
    }

    /**
     * 医生添加分组
     * @param addGroupDTO
     */
    @Override
    public void addGroup(AddGroupDTO addGroupDTO) {
        //获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //添加分组
        int i = mapper.addGroup(addGroupDTO,loginUser.getUserid());
        //判断是否添加成功
        if (i<=0){
            //抛异常
            throw new RuntimeException("添加失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertMessage(MessageRequest request) {
        //获取当前登录人
        Long userId = SecurityUtils.getUserId();
        request.setDoctorId(userId);
        mapper.insertMessage(request.getMessageContent(),request.getDoctorId());
        //记录历史消息记录
        mapper.insertMessageHistory(request.getId(),request.getUserIds());
    }

    @Override
    public List<MessageResponse> selectMessageHistory() {
        //获取当前登录人Id
        Long userId = SecurityUtils.getUserId();
        return mapper.selectMessageHistory(userId);
    }
}
